package net.interneteidee.dao;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Root;

import net.interneteidee.entity.Cliente;
import net.interneteidee.entity.Cliente_;
import net.interneteidee.entity.Ordine;
import net.interneteidee.entity.Ordine_;


@Stateless
public class ClienteDao extends GenericDao<Cliente> {

	
	public ClienteDao() {
		// TODO Auto-generated constructor stub
		super(Cliente.class);	
	}
	
	@PostConstruct
	public void initParameter(){
		
		criteriaBuilder = entityManager.getCriteriaBuilder();
		
		
		
	}
	
	public List<Cliente> findClientiAttivi(){
	
	criteriaQuery = criteriaBuilder.createQuery(Cliente.class);
	root = criteriaQuery.from(Cliente.class);	
		
	GregorianCalendar calendar = new GregorianCalendar(new GregorianCalendar().get(Calendar.YEAR),Calendar.JANUARY,1);
	
	Join<Cliente,Ordine> joinConOrdine = root.join(Cliente_.ordini);
	criteriaQuery.select(root).where(criteriaBuilder.greaterThan(joinConOrdine.get(Ordine_.dataAccettazione),calendar.getTime()));

	
	List<Cliente> clientiAttivi = entityManager.createQuery(criteriaQuery).getResultList();
	
	return 	clientiAttivi;
	}
	
	
}
